define(['../../carrys/cityList'],function(cityData) {
    return ['$scope','$location', '$interval','$timeout', function($scope,$location,$interval,$timeout) {
    	$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','loan'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','62'); 	//显示底部文字
    	
    	$scope.isShow = false;
    	var provinceList = new Array();
    	var cityListTemp = new Array();
    	var districtTemp = new Array();
    	var nplAreaList = new Array();
    	collateData();//整理列表数据
    	
//    	$scope.isNumber=function(value) {
//           var patrn = /^(-)?\d+(\.\d+)?$/;
//           if (patrn.exec(value) == null || value == "") {
//               $scope.$emit("promptAlertMsg","抵押率格式不正确");
//	   		   $scope.$emit("showCollect",true);
//	   		   $timeout(function(){
//   				$scope.$emit("showCollect",false);
//   			   },transData.promptTime);
//           } else {
//               $scope.deductedRate= $scope.deductedRate + "%";
//           }
//    	}
    	
    	var caseTypeList = [
                            {"name":"工业土地","val":'1'},
                            {"name":"农业土地","val":'2'},
                            {"name":"商业土地","val":'3'},
                            {"name":"工业厂房","val":'4'},
                            {"name":"设备","val":'5'},
                            {"name":"住宅","val":'6'},
                            {"name":"商铺","val":'7'},
                            {"name":"写字楼","val":'8'}
                            ];
    	$scope.caseTypeData = "";
    	
    	var tempNplAreaData = {
				"mortgageContent":caseTypeList,
				"mortgagecase": "",
				"mortgageType":"",
				"mortgageTypeList":[
						{"name":"动产抵押","val":'1'},
						{"name":"不动产抵押","val":'2'},
						{"name":"权利抵押","val":'3'},
						{"name":"共同抵押","val":'4'},
						{"name":" 最高额抵押","val":'5'}
						],
				"provinceData": provinceList,
				"cityData":"",
				"districtData":"",
				"province":"",
				"city":"",
				"county":"",
				"address":"",
				"mortgageAmount":""
			};
    	var tempNplAreaString = JSON.stringify(tempNplAreaData);
    	
    	//原债权人姓名
    	$scope.creditor = "";
    	
    	//合作方式1：联合收购；2：联合清收；3：作为中介
    	$scope.collaborateTypeList = [
									{"name":"联合收购","val":'1'},
									{"name":"联合清收","val":'2'},
									{"name":"作为中介","val":'3'}
    	                              ];
    	$scope.collaborateType = "";
    	
    	
    	
    	//放置缓存数据
    	if(NPLCacheData.thirdStepData != null){
    		nplAreaList = NPLCacheData.thirdStepData.nplAreaList;
    		$scope.caseTypeAreaList = NPLCacheData.thirdStepData.nplAreaList;
    		$scope.creditor = NPLCacheData.thirdStepData.creditor;
    		$scope.collaborateType = NPLCacheData.thirdStepData.collaborateType;
	   	}else{
	   		nplAreaList.push(tempNplAreaData);
	   		$scope.caseTypeAreaList = nplAreaList;
	   	}
    	
    	//点击添加
    	$scope.addContentInfo = function(){
    		$timeout(function(){
    			nplAreaList.push(JSON.parse(tempNplAreaString));
//        		$scope.caseTypeAreaList.push(JSON.parse(tempNplAreaString));
    		},500);
    	}
    	
    	//点击删除
    	$scope.delContentInfo = function(){
    		if($scope.isShow){
    			$scope.isShow = false;
    		}else{
    			$scope.isShow = true;
    		}
    	}
    	
    	//点击删除内容
    	$scope.delContent = function(index){
    		if(nplAreaList.length > 1){
    			nplAreaList.splice(index,1);
    		}else{
    			$scope.$emit("promptAlertMsg","至少选中需要一种抵押物！");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}
    		
    	}
    	
    	//点击下一步
    	$scope.goNext = function(){
    		if(! $scope.creditor){
    			$scope.$emit("promptAlertMsg","原债权人不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.collaborateType){
    			$scope.$emit("promptAlertMsg","合作方式不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(judgeList() == "A"){
    			$scope.$emit("promptAlertMsg","请将抵押物详情填写完整");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(judgeList() == "B"){
    			$scope.$emit("promptAlertMsg","抵押物金额格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}
    		saveData();
    	}
    	
    	function judgeList(){
    		var flag = '';
    		for(var i=0;i<nplAreaList.length;i++){
				if(nplAreaList[i].mortgagecase == "" || nplAreaList[i].mortgageType == "" || nplAreaList[i].province == "" || nplAreaList[i].city == "" || nplAreaList[i].county == "" || nplAreaList[i].address == "" || nplAreaList[i].mortgageAmount == ""){
					flag = "A";
					break;
				}
				if(isNaN(nplAreaList[i].mortgageAmount)){
					flag = "B";
					break;
				}
			}
    		return flag;
    	}
    	
    	
    	//保存数据
    	function saveData(){
    		NPLCacheData.thirdStepData = {};
    		NPLCacheData.thirdStepData.nplAreaList = nplAreaList;
    		NPLCacheData.thirdStepData.creditor = $scope.creditor;
    		NPLCacheData.thirdStepData.collaborateType = $scope.collaborateType;
    		
    		$scope.$emit("goNext","/applyNplFourth");
    	}
        
        //点击查看不良资产查看清单
        $scope.checkNplList = function(){
        	$scope.detslide_up = true;
        }
    	
    	//显示省份
    	
    	//显示城市
    	$scope.clickCity = function(data,flag){
    		var cityList = new Array();
    		var provinceName = data.province;
    		for(var i=0;i<cityListTemp.length;i++){
    			if(cityListTemp[i].province == provinceName){
    				var tempData = {'city': cityListTemp[i].city};
    				cityList.push(tempData);
    			}
    		}
    		nplAreaList[flag].cityData = cityList;
//    		$scope.caseTypeAreaList = areaArray;
    	}
    	//显示地区
    	$scope.clickArea = function(data,flag) {
    		var districtList = new Array();
    		var districtList1 = new Array();
    		var cityName = data.city;
    		for(var i=0;i<districtTemp.length;i++){
    			if(districtTemp[i].city == cityName){
    				districtList = districtTemp[i].district;
    				break;
    			}
    		}
    		for(var i=0;i< districtList.length;i++){
    			var tempData = {'district':districtList[i]};
    			districtList1.push(tempData);
    		}
    		nplAreaList[flag].districtData = districtList1;
//    		$scope.caseTypeAreaList = areaArray;
    	}
    	
    	//整理省份数据
    	function collateData() {
    		cityListTemp = new Array();
    		districtTemp = new Array();
			for(var key in cityData.addressDetail){
				var tempData = {"province":key};
				provinceList.push(tempData);
				collateCity(key,cityData.addressDetail[key]);
			}
    	}
    	//整理城市数据
		function collateCity(data1,data2) {
			for(var key in data2){
				var tempList = {'province':data1,'city': key};
				cityListTemp.push(tempList);
				districtCity(key,data2[key]);
			}
		}
		//整理县区数据
		function districtCity(data1,data2){
			var tempList = {'city': data1,'district': data2};
			districtTemp.push(tempList);
		}
		
    	$scope.$apply();
    }];
});